﻿using Microsoft.AspNetCore.Authorization;

namespace Simple.WebApi.Controllers.System
{
    /// <summary>
    /// 用户管理
    /// </summary>
    [Route("api/SysClasses/[action]")]
    [ApiController]
    [Authorize]
    public class ClassesController : ControllerBase
    {
        private readonly ClassesService _classesService;

        public ClassesController(ClassesService classesService)
        {
            _classesService = classesService;
        }

        /// <summary>
        /// 当前老师管理班级列表（未归档班级）
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<AppResult> ListByTeacher()
        {
            List<ClassesModel> data = await _classesService.GetByTeacher();
            return AppResult.Status200OK(data: data);
        }

        /// <summary>
        /// 班级列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<AppResult> List([FromQuery] bool? isArchived)
        {
            List<ClassesModel> data = await _classesService.GetAsync(isArchived);
            return AppResult.Status200OK(data: data);
        }

        /// <summary>
        /// 班级查询
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<AppResult> Page([FromQuery] ClassesPageInputModel model)
        {
            PageResultModel<ClassesModel> data = await _classesService.GetPageAsync(model);
            return AppResult.Status200OK(data: data);
        }

        /// <summary>
        /// 班级增加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> Add([FromBody] ClassesModel model)
        {
            await _classesService.AddAsync(model);
            return AppResult.Status200OK("新增成功");
        }

        /// <summary>
        /// 班级编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> Edit([FromBody] ClassesModel model)
        {
            await _classesService.UpdateAsync(model);
            return AppResult.Status200OK("更新成功");
        }

        /// <summary>
        /// 班级删除
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> Delete([FromBody] List<IdInputModel> models)
        {
            await _classesService.DeleteAsync(models.Select(m => m.Id));
            return AppResult.Status200OK("删除成功");
        }

        /// <summary>
        /// 班级花名册
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<AppResult> Roster([FromQuery] RosterPageInputModel input)
        {
            PageResultModel<ChildrenModel> data = await _classesService.Roster(input);
            return AppResult.Status200OK(data: data);
        }

        /// <summary>
        /// 班级归档
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<AppResult> Archive([FromBody] List<IdInputModel> input)
        {
            await _classesService.ArchiveAsync(input.Select(m => m.Id));
            return AppResult.Status200OK("归档成功");
        }

        /// <summary>
        /// 归档信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<AppResult> Archiveinfo([FromQuery] ClassesPageInputModel model)
        {
            PageResultModel<ClassesModel> data = await _classesService.Archiveinfo(model);
            return AppResult.Status200OK(data: data);
        }
    }
}
